package cn.store.web.servlet;

import cn.store.domain.Order;
import cn.store.domain.PageModel;
import cn.store.service.OrderService;
import cn.store.service.ProductService;
import cn.store.service.serviceImp.OrderServiceImp;
import cn.store.service.serviceImp.ProductServiceImp;
import cn.store.web.base.BaseServlet;
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONArray;
import redis.clients.jedis.Protocol;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Key;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminOrderServlet extends BaseServlet {

	/**
	 * 带分页的订单查询
	 * */
	public String findOrdersWithPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		OrderService OrderService=new OrderServiceImp();
		//获取当前页
		int curNum;
		if(req.getParameter("num") == null){
			curNum = 1;
		}else{
			curNum=Integer.parseInt(req.getParameter("num"));
		}
		//获取查询订单状态
		String st=req.getParameter("state");
		//获取查询词
		String keyWord = req.getParameter("keyWord");
		PageModel pm;
		if(st == null||"".equals(st)){
			st = null;
		}
		if(keyWord != null){
			keyWord = keyWord.trim();
			if(keyWord.equals("")) keyWord = null;
		}
		pm=OrderService.findAllOrders(st,curNum,keyWord);
		if(st == null){
			pm.setUrl("/AdminOrderServlet?method=findOrdersWithPage");
		}else{
			pm.setUrl("/AdminOrderServlet?method=findOrdersWithPage&state=" + st);
		}
		//将全部订单放入request
		req.setAttribute("page", pm);
		req.setAttribute("keyWord",keyWord);
		return "/admin/order/list.jsp";
	}

	//Ajax:查询订单明细
	public String findOrderByOidWithAjax(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//服务端获取到订单ID,
		String oid=req.getParameter("id");
		//查询这个订单下所有的订单项以及订单项对应的商品信息,返回集合
		OrderService OrderService=new OrderServiceImp();
		Order order=OrderService.findOrderByOid(oid);
		//将返回的集合转换为JSON格式字符串,响应到客户端
		String jsonStr=JSONArray.fromObject(order.getList()).toString();
		//响应到客户端
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().println(jsonStr);
		return null;
	}
	
	//updateOrderByOid
	public String updateOrderByOid(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//获取订单ID
		String oid=req.getParameter("oid");
		//根据订单ID查询订单
		OrderService OrderService=new OrderServiceImp();
		Order order=OrderService.findOrderByOid(oid);
		//设置订单状态
		order.setState(3);
		//修改订单信息
		OrderService.updateOrder(order);
		//重新定向到查询已发货订单
		resp.sendRedirect(req.getContextPath()+"/AdminOrderServlet?method=findOrdersWithPage&state=3");
		return null;
	}
	/**
	 * 通过id删除订单
	 * return json
	 * */
	public String deleteOrderById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String oid = req.getParameter("oid");
		Map<String,String> msg = new HashMap<>();
		resp.setContentType("application/json; charset=utf-8");
		PrintWriter p =resp.getWriter();
		try {
			/*尝试删除*/
			OrderService orderService = new OrderServiceImp();
			if(orderService.deleteOrderById(oid) > 0){
				msg.put("flag","true");
			}else{
				msg.put("flag","false");
			}

		}catch (Exception e){
			e.printStackTrace();
			/*删除失败*/
			msg.put("flag","false");
		}finally {
			String jsonStr = JSON.toJSONString(msg);
			System.out.println(jsonStr);
			p.write(jsonStr);
			return null;
		}

	}

}
